package com.fhz.day004;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.work.BackoffPolicy;
import androidx.work.Constraints;
import androidx.work.Data;
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;

import android.os.Bundle;
import android.util.Log;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "feng";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //反射
//        Class<? extends Book> aClass = new Book().getClass();
//        Class<? extends Book> aClass = Book.class;


        //任务的约束
        Constraints build = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .setRequiresBatteryNotLow(true)
                .build();


        //只执行一次的任务
        OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(MyWork.class)
//                .setConstraints(build)
                .setInitialDelay(5, TimeUnit.SECONDS)
                .setInputData(new Data.Builder().putString("name","小明").build())
//                .setBackoffCriteria(
//                        BackoffPolicy.LINEAR,
//                        OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
//                        TimeUnit.MILLISECONDS)
                .addTag("吃饭")
                .build();

        OneTimeWorkRequest request1 = new OneTimeWorkRequest.Builder(MyWork.class)
//                .setConstraints(build)
                .setInitialDelay(5, TimeUnit.SECONDS)
                .setInputData(new Data.Builder().putString("name","小明").build())
//                .setBackoffCriteria(
//                        BackoffPolicy.LINEAR,
//                        OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
//                        TimeUnit.MILLISECONDS)
                .addTag("洗澡")
                .build();

//        PeriodicWorkRequest build1 = new PeriodicWorkRequest.Builder(MyWork.class, 1, TimeUnit.DAYS)
//                .setConstraints(build)
//                .setInitialDelay(5, TimeUnit.SECONDS)
//                .build();

        //workmanager
//        WorkManager.getInstance(this).enqueue(request);

//        WorkManager.getInstance(this).beginWith(Arrays.asList(request, request1)).enqueue();




        WorkManager.getInstance(this).getWorkInfoByIdLiveData(request.getId()).observe(this, new Observer<WorkInfo>() {
            @Override
            public void onChanged(WorkInfo workInfo) {
                Log.i(TAG, "onChanged: "+workInfo.getProgress());
                if (workInfo != null) {
//                    Data progress = workInfo.getProgress();
                    Data outputData = workInfo.getOutputData();
                    int value = outputData.getInt("PROGRESS", 0);
                    // Do something with progress
                    Log.i(TAG, "onChanged: --"+value);
                }
            }
        });
//        WorkManager.getInstance(this).cancelAllWork();
//        WorkManager.getInstance(this).cancelAllWorkByTag("吃饭");



    }

    private void extracted() {
        User user = new User.Builder()
                .setName("xiaoming")
                .setAddress("dizhi")
                .setEmail("eamil")
                .build();
    }

}